#include <func.h>

int main()
{
    //创建UDP套接字
    int clientfd = socket(AF_INET, SOCK_DGRAM, 0);
    ERROR_CHECK(clientfd, -1, "socket");

    //设置服务器的网络地址
    struct sockaddr_in serveraddr;
    memset(&serveraddr, 0, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_port = htons(8080);
    serveraddr.sin_addr.s_addr = inet_addr("192.168.30.129");
    socklen_t len = sizeof(serveraddr);

    //第一次发送数据
    const char * msg = "hello, server";
    int ret = sendto(clientfd, msg, strlen(msg), 0,
           (const struct sockaddr*)&serveraddr, sizeof(serveraddr));
    printf("1st sendto %d bytes.\n", ret);

    //第二次发送数据
    ret = sendto(clientfd, msg, strlen(msg), 0,
           (const struct sockaddr*)&serveraddr, sizeof(serveraddr));
    printf("2nd sendto %d bytes.\n", ret);

    //接收数据
    char buff[100] = {0};
    recvfrom(clientfd, buff, sizeof(buff), 0,
             (struct sockaddr*)&serveraddr, &len);
    printf("recvfrom from server: %s\n", buff);

    //关闭套接字
    close(clientfd);

    return 0;
}

